const fs = require('fs');
const googleTTS = require('google-tts-api');

// 1. get audio URL
// const url = googleTTS.getAudioUrl('你好世界', { lang: 'zh-TW' });
// console.log({ url }); // https://translate.google.com/translate_tts?...

// 2. get base64 text
googleTTS
  .getAudioBase64('小逼崽子', { lang: 'zh-TW' })
  .then((base64) => {
    // console.log({ base64 });

    // save the audio file
    const buffer = Buffer.from(base64, 'base64');
    fs.writeFileSync('123.mp3', buffer, { encoding: 'base64' });
  })
  .catch(console.error);